package com.spring.student.systemDB;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.DOMImplementation;

@Component("courseManage")
public class CourseManageImpl implements CourseManage{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void updateCourse(String courseName){
        String sql = "update course set"+courseName+"="+courseName+"-1";
        int num = jdbcTemplate.update(sql);
        if(num<=0)
            System.out.println("更新待选课程记录失败");
    }
    @Override
    public void queryCourse(){
        String sql = "select * from courses";
        RowMapper<Course>rowMapper;
        rowMapper = new BeanPropertyRowMapper<Course>(Course.class);
        Course course=jdbcTemplate.queryForObject(sql,rowMapper);
        System.out.println(course);
    }
    @Override
    @Transactional(propagation=Propagation.REQIRED,readOnly=false,isolation=Isolation.DEFAULT)
    public void selectCourse(String number,String courseName){
        updateCourse(courseName);
        queryCourse();
        jdbcTemplate.execute("create table if not exists optional_courses("
                +"number varchar(20),courseName varchar(20),score double)");
        String sql = "insert into optional_courses(number,courseName,score) values(?,?,?)";
        jdbcTemplate.update(sql,number,courseName,0);
        System.out.println("查询选课信息记录");
        sql = "select * from optional_courses where number=?";
        List<Map<String,Object>>maps = jdbcTemplate.queryForList(sql,number);
        for(Map<String,Object>map:maps);
    }
}
